package com.smartwebx.ha.dal.realtime.dao;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.smartwebx.ha.dal.config.OracleConfigInfo;
import com.smartwebx.ha.dal.usebean.GisBase;
import com.smartwebx.ha.dal.usebean.ShowStationInfo;
import com.smartwebx.ha.dal.usebean.StationAdminArea;
import com.smartwebx.ha.dal.usebean.StationInfo;

@CacheNamespace()
public interface StationMapper {
	String ST_STBPRP_B = OracleConfigInfo.REALTIME_NAME + ".ST_STBPRP_B";
	String ST_PPTN_R = OracleConfigInfo.REALTIME_NAME + ".ST_PPTN_R";
	String JS_YLZQ =OracleConfigInfo.REALTIME_NAME +".JS_YLZQ";
	

	@Select("SELECT * FROM " + ST_STBPRP_B + " WHERE STCD=#{stcd}")
	public StationInfo getStationInfo(@Param("stcd") String stcd);

	/**
	 * 获得时间点之前的所有上报雨量的站码信息
	 * 
	 * @param datePoint
	 * @return
	 */
	@Select("SELECT DISTINCT STCD FROM "
			+ ST_PPTN_R
			+ " WHERE TM > #{datePoint} AND STCD IS NOT NULL AND LENGTH(RTRIM(STCD))>=8")
	public Collection<String> getDatePointBeforeWaterStationStcdFilter(
			@Param("datePoint") Date datePoint);

	/**
	 * 站码基本信息
	 * 
	 * @return
	 */
	@Select("SELECT STCD,ADDVCD inProv,STTP,FRGRD,STNM,LGTD,LTTD FROM "
			+ ST_STBPRP_B
			+ " WHERE STCD IN (#{stcds,typeHandler=InQueryHandler}) AND LGTD IS NOT NULL AND LTTD IS NOT NULL AND ADDVCD IS NOT NULL AND FRGRD IS NOT NULL AND STTP IS NOT NULL")
	@MapKey("stcd")
	public Map<String, GisBase> getStatBaseInfoInQuery(
			@Param("stcds") Iterable<String> stcds);
	
	/**
	 * 查找所有站码的基本信息
	 * @return
	 */
	@Select("SELECT STCD,ADDVCD inProv,STTP,FRGRD,STNM,LGTD,LTTD FROM "
			+ ST_STBPRP_B
			+ " WHERE LGTD IS NOT NULL AND LTTD IS NOT NULL")
	@MapKey("stcd")
	public Map<String,GisBase> getAllStatInfo();
	
	@Select("SELECT COUNT(STCD) FROM " + ST_STBPRP_B
			+ " WHERE STCD<>#{randomKey}")
	public Integer getStatBaseCount(@Param("randomKey") String randomKey);

	@Select("SELECT stcd id,STNM name,ADDVCD parentId,1 statType FROM "
			+ ST_STBPRP_B + " WHERE LGTD IS NOT NULL AND LTTD IS NOT NULL")
	public Collection<StationAdminArea> getAllBaseStationInfo();

	@Select("SELECT stcd id,STNM name,ADDVCD parentId,1 statType FROM "
			+ ST_STBPRP_B
			+ " WHERE STCD IN (#{stcds,typeHandler=InQueryHandler})")
	public Collection<StationAdminArea> getBaseStationInfosInQuery(
			@Param("stcds") Iterable<String> stcds);

	@Select("SELECT * FROM " + ST_STBPRP_B
			+ " WHERE STCD IN (#{stcds,typeHandler=InQueryHandler})")
	public Collection<ShowStationInfo> getStationInfosInQuery(
			@Param("stcds") Iterable<String> stcds);
	
	/**
	 * 查找100、150、188、280站
	 * @return
	 */
	@Select("SELECT STCD FROM " + JS_YLZQ
			+" WHERE PURPOSE LIKE CONCAT('%',CONCAT(#{key},'%')) AND ASTNM LIKE '全省%'")
	public List<String> getGisStations(@Param("key") String key);

	/**
	 * 查找150站
	 * @return
	 */
	@Select("SELECT STCD FROM " + JS_YLZQ
			+" WHERE PURPOSE LIKE '雨量150%' AND ASTNM LIKE '全省%'")
	public List<String> getGis150Stations();
	
	/**
	 * 查找188站
	 * @return
	 */
	@Select("SELECT STCD FROM " + JS_YLZQ
			+" WHERE PURPOSE LIKE '雨量188%' AND ASTNM LIKE '全省%'")
	public List<String> getGis188Stations();

}